<?php namespace JiwaNala\Otentikasi;

use Illuminate\Auth\UserProviderInterface;

class PenyediaAkun implements \Illuminate\Auth\UserProviderInterface {

    /**
    * @var UserService
    */
    private $model;

    public function __construct(\JiwaNala\Entitas\Inti\Akun $model){
        $this->model = $model;
    } 

    /**
	 * Retrieve a user by their unique identifier.
	 *
	 * @param  mixed  $identifier
	 * @return \JiwaNala\Entitas\Inti\Akun;
	 */
	public function retrieveById($identifier){
		return $this->createModel()->newQuery()->find($identifier);
	}
	
	/**
	 * Retrieve a user by their credentials.
	 *
	 * @param  mixed  $identifier
	 * @return \JiwaNala\Entitas\Inti\Akun;
	 */
    public function retrieveByCredentials(array $credentials){
		$query = $this->createModel()->newQuery();

		foreach ($credentials as $key => $value)
		{
			if ( ! str_contains($key, 'sandi')) $query->where($key, $value);
		}

		return $query->first();
	}
	
	public function createModel() {
		$class = '\\'.ltrim($this->model, '\\');

		return new $class;
	}

    /**
     * Validate a user against the given credentials.
     *
     * @param \Illuminate\Auth\UserInterface $user
     * @param  array  $credentials
     *
     * @return bool
     */
     public function validateCredentials(\Illuminate\Auth\UserInterface $user, array $credentials)
     {
        $plain = $credentials['sandi'];

		return Hash::check($plain, $user->getAuthPassword());
     }
}